﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyFloridaHouse.libPrint.KML;
using MyFloridaHouse.LibDTO;
using System.Data.SqlClient;
using Microsoft.SqlServer.Types;
using System.Data.SqlTypes;
using MyFloridaHouse.libPrint.BingMapAjaxHelpers;
using MyFloridaHouse.DataAccess;

namespace MyFloridaHouse.libPrint.MapBaseLayers
{
    public static class MapBase
    {

        public static kmlDocument GetCountiesBaseShapes()
        {
            //
            // First, the program accesses the connection string and uses it on a connection.
            //
            List<ShapeDTO> shapeList = new List<ShapeDTO>();

            DAL_CensusData dal = new DAL_CensusData();
            var queryResults2 = dal.GetCountyShapes();
            shapeList = (from q in queryResults2 select q).ToList();	//executes immediately (not lazy loading)

            kmlDocument finalKMLDoc = new kmlDocument();
            finalKMLDoc.Placemark = new kmlDocumentPlacemark[shapeList.Count]; //initializes the array
            finalKMLDoc.name = "Florida Counties";
            int counter = 0;
            SqlGeometry newGeom;
            SqlChars sc;
            try
            {
                foreach (ShapeDTO shape in shapeList)
                {
                    SqlString kmlShape = KMLFunctions.ExportWKTToKml((SqlString)shape.wkt);
                    kml kmlShapeDeserialized = KMLSerialFunctions.DeserializeKML(kmlShape.ToString().Replace(@"\", String.Empty));
                    sc = new SqlChars(shape.wkt.ToString());
                    newGeom = SqlGeometry.STGeomFromText(sc, 4326);
                    kmlDocumentPlacemark newPlaceMark = new kmlDocumentPlacemark();
                    newPlaceMark.name = shape.name;
                    newPlaceMark.MultiGeometry = kmlShapeDeserialized.Items[0].Placemark[0].MultiGeometry;
                    newPlaceMark.Polygon = kmlShapeDeserialized.Items[0].Placemark[0].Polygon;
                    kmlDocumentPlacemarkExtendedData newExtDat = new kmlDocumentPlacemarkExtendedData();
                    kmlLatLonAltBox newLatLonBox = new kmlLatLonAltBox();
                    newLatLonBox.north = newGeom.STEnvelope().STPointN(4).STY.ToString();
                    newLatLonBox.west = newGeom.STEnvelope().STPointN(4).STX.ToString();
                    newLatLonBox.south = newGeom.STEnvelope().STPointN(2).STY.ToString();
                    newLatLonBox.east = newGeom.STEnvelope().STPointN(2).STX.ToString();
                    newExtDat.latLonAltBox = newLatLonBox;
                    kmlPoint centerPoint = new kmlPoint();
                    //Point labelPoint = JavaScriptBuilder.GetLabelLocation(newGeom);
                    Point labelPoint = new Point();
                    labelPoint.Longitude = (double)shape.centerLngX;
                    labelPoint.Latitude = (double)shape.centerLatY;
                    centerPoint.coordinates = "" + labelPoint.Longitude + "," + labelPoint.Latitude + "";
                    newExtDat.centerPoint = centerPoint;
                    newPlaceMark.ExtendedData = newExtDat;
                    finalKMLDoc.Placemark[counter] = newPlaceMark;
                    counter++;
                }
            }
            catch (Exception ex)
            {
                string errMsg = ex.ToString();
            }

            return finalKMLDoc;
        }
    }
}